package com.wandian.texas.demo;

import java.util.concurrent.PriorityBlockingQueue;
import java.util.concurrent.TimeUnit;

import com.wandian.executor.TimerCommand;
import com.wandian.texas.BaseCommandObject;

/**
 * 用PriorityBlockingQueue简化线程优先级调度策略
 * 
 * @author long
 * @Create 2012-10-12 下午5:48:48
 * @QQ 108232706
 * @version 1.0
 * @description ThreadPriorityScheduleDemo
 */
public class ThreadPriorityScheduleDemo {

	static class Bus implements Comparable<Bus> {

		public String busNo;
		public Integer busType;
		public Integer level;

		public Bus() {
		}

		private Bus(String busNo, Integer busType, Integer level) {
			super();
			this.busNo = busNo;
			this.busType = busType;
			this.level = level;
		}

		// level 越小,优先级越高,只要返回-1
		public int compareTo(Bus o) {
			if (o instanceof Bus) {
				return level > o.level ? 1 : -1;
			}
			return 0;
		}

		@Override
		public String toString() {
			return "当前车信息：种类[" + busType + "]车牌[" + busNo + "]优先级[" + level + "]";
		}
	}

	private static final int MAX_PARKING = 10;
	private static final PriorityBlockingQueue<Bus> busStation = new PriorityBlockingQueue<Bus>(MAX_PARKING);
	private static final PriorityBlockingQueue<BaseCommandObject> cmdStation = new PriorityBlockingQueue<BaseCommandObject>(MAX_PARKING);

	public static void testPriorityBlockingQueue() {

		busEnter(new Bus("粤A12345", 2, 5));
		busEnter(new Bus("粤A88888", 1, 2));
		busEnter(new Bus("粤A66666", 2, 6));
		busEnter(new Bus("粤A33333", 1, 7));
		busEnter(new Bus("粤A21123", 2, 1));
		busEnter(new Bus("粤AGG892", 1, 4));
		busEnter(new Bus("粤AJJ000", 2, 9));
		while (true) {
			busQuit();
		}
	}

	public static void testCmdPriorityBlockingQueue() {

		cmdEnter(new TimerCommand("1", 1000L, 1000L, 2));
		cmdEnter(new TimerCommand("2", 4000L, 1000L, 1));
		cmdEnter(new TimerCommand("3", 2000L, 1000L, 2));
		cmdEnter(new TimerCommand("4", 3000L, 1000L, 3));
		cmdEnter(new TimerCommand("5", 100L, 1100L, 5));
		cmdEnter(new TimerCommand("6", 200L, 1200L, 5));
		cmdEnter(new TimerCommand("7", 300L, 1300L, 5));
		while (true) {
			cmdQuit();
		}
	}

	/**
	 * 车辆进站
	 * 
	 * @param bus
	 */
	private static void cmdEnter(BaseCommandObject cmd) {
		// System.out.println("进站-->"+bus.toString());
		if (cmdStation.size() < MAX_PARKING) {
			cmd.start();
			cmdStation.add(cmd);
		}
		else
			System.out.println("cmd位已满");

	}

	/**
	 * 车辆出站
	 */
	private static void cmdQuit() {
		try {
			BaseCommandObject cmd = cmdStation.peek();
			if (cmd != null) {
				if (cmd.runTime <= System.nanoTime()) {
					cmd = cmdStation.take();
					System.out.println("出站-->" + cmd.toString());
					// 出站之后，判断是否执行完毕
					if (!cmd.complete()) {
						if (cmdStation.size() < MAX_PARKING) {
							cmdStation.add(cmd);
						}
						else
							System.out.println("无法入栈,cmd位已满");
					} else {
						System.out.println("完毕-->" + cmd.toString());
					}
				}
			}
			TimeUnit.MILLISECONDS.sleep(1);

		} catch (InterruptedException e) {

		}
	}

	/**
	 * 车辆进站
	 * 
	 * @param bus
	 */
	private static void busEnter(Bus bus) {
		// System.out.println("进站-->"+bus.toString());
		if (busStation.size() < MAX_PARKING)
			busStation.add(bus);
		else
			System.out.println("站内车位已满");

	}

	/**
	 * 车辆出站
	 */
	private static void busQuit() {
		try {
			Bus bus = busStation.take();
			System.out.println("出站-->" + bus.toString());
		} catch (InterruptedException e) {

		}
	}
}
